<template>
  <el-form-item label="车牌号码">
    <el-input style="width: 300px;" v-model="fen.palteNumber" placeholder="请输入车牌号"/>
  </el-form-item>
  <el-button size="small" @click="processList()">
    搜索
  </el-button>
  <el-button size="small" @click="dialogVisible=true">
    添加
  </el-button>
  <el-table
    ref="singleTableRef"
    :data="tableData"
    highlight-current-row
    style="width: 100%"
    @current-change="handleCurrentChange"
  >
    <el-table-column type="index" width="50" />
    <el-table-column property="palteNumber" label="车牌号码" width="125" />
    <el-table-column property="status" label="车牌是否有效" width="125" #default="scope">
      <el-text type="warning" v-if="scope.row.status == 0" >有效</el-text>
      <el-text type="info" v-if="scope.row.status == 1">无效</el-text>
    </el-table-column>
    <el-table-column property="area" label="登记区域" width="120" />
    <el-table-column property="cameraId" label="采集点摄像头id" width="122" />
    <el-table-column property="userId" label="绑定的用户" width="120" />
    <el-table-column property="createTime" label="创建时间" width="120" />
    <el-table-column property="updateTime" label="更新时间" width="120" />
    <el-table-column label="操作">
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.row)">
          修改
        </el-button>
        <el-button
          size="small"
          type="danger"
          @click="handleDelete(scope.row)">
          删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <!--  分页-->
  <el-pagination
    v-model:current-page="fen.pageNum"
    v-model:page-size="fen.pageSize"
    :page-sizes="[5, 10, 15, 20]"
    layout="total, sizes, prev, pager, next, jumper"
    :total=total
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
  />
  <!--  添加弹出框-->
  <el-dialog
    v-model="dialogVisible"
    title="添加"
    width="500px"
    :before-close="handleClose">
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      label-width="auto"
      class="demo-ruleForm"
      status-icon
    >
      <el-form-item label="车牌号码" prop="palteNumber">
        <el-input v-model="ruleForm.palteNumber" />
      </el-form-item>
      <el-form-item label="车牌是否有效" prop="status">
        <el-input v-model="ruleForm.status" />
      </el-form-item>
      <el-form-item label="登记区域" prop="area">
        <el-input v-model="ruleForm.area" />
      </el-form-item>
      <el-form-item label="采集点摄像头id" prop="cameraId">
        <el-input v-model="ruleForm.cameraId" />
      </el-form-item>
      <el-form-item label="绑定的用户" prop="userId">
        <el-input v-model="ruleForm.userId" />
      </el-form-item>
    </el-form>
    <el-form-item>
      <el-button type="primary" @click="submitForm()">
        添加
      </el-button>
      <el-button @click="resetForm()">重置</el-button>
    </el-form-item>
  </el-dialog>
  <!--  修改弹出框-->
  <el-dialog
    v-model="dialogVisible1"
    title="修改"
    width="500px"
    :before-close="handleClose">
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm1"
      label-width="auto"
      class="demo-ruleForm"
      status-icon
    >
      <el-form-item label="id" prop="id">
        <el-input v-model="ruleForm1.id" />
      </el-form-item>
      <el-form-item label="车牌号码" prop="palteNumber">
        <el-input v-model="ruleForm1.palteNumber" />
      </el-form-item>
      <el-form-item label="车牌是否有效" prop="status">
        <el-input v-model="ruleForm1.status" />
      </el-form-item>
      <el-form-item label="登记区域" prop="area">
        <el-input v-model="ruleForm1.area" />
      </el-form-item>
      <el-form-item label="采集点摄像头id" prop="cameraId">
        <el-input v-model="ruleForm1.cameraId" />
      </el-form-item>
      <el-form-item label="绑定的用户" prop="userId">
        <el-input v-model="ruleForm1.userId" />
      </el-form-item>
    </el-form>
    <el-form-item>
      <el-button type="primary" @click="submitForm1()">
        修改
      </el-button>
      <el-button @click="resetForm1()">重置</el-button>
    </el-form-item>
  </el-dialog>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from "element-plus";
import { add, del, getAll, update } from "@/api/automatic";


const fen=ref({
  pageNum:1,
  pageSize:5,
  palteNumber:''
})

const total=ref()
const handleSizeChange = (val) => {
  fen.value.pageSize=val
  processList()
}
const handleCurrentChange = (val) => {
  fen.value.pageNum=val
  processList()
}
// 查询所有
const tableData = ref()
const processList =()=>{
  return getAll(fen.value).then(res=>{
    console.log(res.data);
    tableData.value = res.data.records
    total.value=res.data.total
  })
}
processList();
// 添加
const ruleForm = ref({
  palteNumber: null,
  status:null,
  area:null,
  cameraId:null,
  userId:null,
})

const dialogVisible = ref(false)
const submitForm=()=>{
  return add(ruleForm.value).then(res=>{
    console.log(res);
    if(ruleForm.value.palteNumber != null){
      if (res.data.code === 200) {
        dialogVisible.value=false
        ElMessage.error(res.msg)
      } else {
        dialogVisible.value=false
        ElMessage.success(res.msg)
      }
    }else {
      ElMessage.error("添加失败")
    }

  })
}
const resetForm = () => {
  ruleForm.value={}
}
// 删除
const handleDelete = (row) => {
  console.log(row.id);
  if (confirm("是否删除？")){
    return del(row.id).then(res=>{
      ElMessage.error(res.msg)
    })
  }else {
    ElMessage.info("已经取消")
  }
}
// 修改
const resetForm1 = () => {
  ruleForm1.value={}
}

const ruleForm1 = ref({
  id:"",
  palteNumber: "",
  status:"",
  area:"",
  cameraId:"",
  userId:"",
})
const dialogVisible1 = ref(false)

function handleEdit(row){
  dialogVisible1.value=true
  console.log(row+"row");
  console.log(ruleForm1.value+"ruleForm1");
  ruleForm1.value.id = row.id;
  ruleForm1.value.palteNumber = row.palteNumber;
  ruleForm1.value.status = row.status;
  ruleForm1.value.area = row.area;
  ruleForm1.value.cameraId = row.cameraId;
  ruleForm1.value.userId = row.userId;
}
const submitForm1 = () => {
  console.log(ruleForm1.value);
  update(ruleForm1.value).then(res=>{
    console.log(res);
    if (res.data.code === 200) {
      dialogVisible1.value=false
      ElMessage.error(res.msg)
    } else {
      dialogVisible1.value=false
      ElMessage.success(res.msg)
    }
  })
}



</script>
